cmake_minimum_required(VERSION 3.10)
project(protocol_test C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

add_compile_options(-Wall -Wextra -g)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..)

set(UART_PROTOCOL_SRCS
    ${CMAKE_CURRENT_SOURCE_DIR}/../protocol.c
    ${CMAKE_CURRENT_SOURCE_DIR}/../port_posix.c
)


add_library(protocol STATIC ${UART_PROTOCOL_SRCS})
target_include_directories(protocol PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)

add_executable(demo ${CMAKE_CURRENT_SOURCE_DIR}/../examples/protocol_demo.c)
target_link_libraries(demo protocol)


add_custom_target(run_tests_c 
    COMMAND echo "Configuring test environment and running tests..."
    COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/scripts/setup_test.sh
    COMMAND sleep 1
    COMMAND ${CMAKE_CURRENT_BINARY_DIR}/demo /tmp/ttyVSP2 &
    COMMAND ${CMAKE_CURRENT_BINARY_DIR}/demo /tmp/ttyVSP1 &
    COMMAND sleep 5
    COMMAND echo "Test finished"
    COMMAND pkill -f demo || true
    COMMAND kill `cat /tmp/socat.pid` || true
    COMMAND rm -f /tmp/socat.pid
    DEPENDS demo
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

add_custom_target(run_tests_python 
    COMMAND echo "Configuring test environment and running tests..."
    COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/scripts/setup_test.sh
    COMMAND sleep 1
    COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/../examples/protocol_demo.py /tmp/ttyVSP2 115200 &
    COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/../examples/protocol_demo.py /tmp/ttyVSP1 115200 &
    COMMAND sleep 5
    COMMAND echo "Test finished"
    COMMAND pkill -f protocol_demo.py || true
    COMMAND kill `cat /tmp/socat.pid` || true
    COMMAND rm -f /tmp/socat.pid
    DEPENDS demo
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

add_custom_target(run_tests_c_python 
    COMMAND echo "Configuring test environment and running tests..."
    COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/scripts/setup_test.sh
    COMMAND sleep 1
    COMMAND ${CMAKE_CURRENT_BINARY_DIR}/demo /tmp/ttyVSP2 &
    COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/../examples/protocol_demo.py /tmp/ttyVSP1 115200 &
    COMMAND sleep 5
    COMMAND echo "Test finished"
    COMMAND pkill -f demo || true
    COMMAND pkill -f protocol_demo.py || true
    COMMAND kill `cat /tmp/socat.pid` || true
    COMMAND rm -f /tmp/socat.pid
    DEPENDS demo
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
